package com.web.controller;

import com.util.SessionUtils;
import com.web.dto.UserInfoDTO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * @author yangguanzan
 * @date 9/20/21
 * @description
 */
@Controller
@RequestMapping("/hospital")
public class IndexController {


    @RequestMapping(value =  {"index",""} , method = RequestMethod.GET)
    public String indexPage(ModelMap model,HttpServletRequest req) {
        HttpSession session = req.getSession(false);
        UserInfoDTO userInfoDTO = (UserInfoDTO) SessionUtils.getUser(session);
        if(null!=userInfoDTO){
            model.addAttribute("username",userInfoDTO.getUsername());
        }
        return "index";
    }

    @GetMapping(value = "logout")
    public String logout(HttpServletRequest req) {
        //先判断是否存在已登陆的session
        HttpSession session = req.getSession(false);
        if (SessionUtils.hasUserSession(session)) {
            //删除当前登录的用户信息
            //本次会话的其他信息还是保存在内存中,没有及时清理
            //SessionUtils.removeUser(req.getSession());
            //销毁整个session
            session.invalidate();
        }
        //重定向登录页
        return "redirect:login";
    }
}
